package com.example.backend.controller;

import com.example.backend.dto.WebSocketMessageDto;
import com.example.backend.domain.enums.WebSocketMessageType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.stereotype.Controller;

import java.security.Principal;
import java.time.LocalDateTime;
import java.util.UUID;

/**
 * WebSocket消息处理器
 * 处理客户端发送的WebSocket消息
 */
@Slf4j
@Controller
public class WebSocketMessageController {

    /**
     * 处理心跳消息
     */
    @MessageMapping("/ping")
    @SendToUser("/queue/pong")
    public WebSocketMessageDto handlePing(@Payload WebSocketMessageDto message, 
                                         Principal principal, 
                                         SimpMessageHeaderAccessor headerAccessor) {
        log.info("收到心跳消息 from user: {}", principal.getName());
        
        return WebSocketMessageDto.builder()
                .messageId(UUID.randomUUID().toString())
                .type(WebSocketMessageType.PONG)
                .title("心跳响应")
                .content("服务器正常运行")
                .timestamp(LocalDateTime.now())
                .priority(1)
                .requiresAck(false)
                .build();
    }

    /**
     * 处理消息确认
     */
    @MessageMapping("/ack")
    public void handleAck(@Payload WebSocketMessageDto message, Principal principal) {
        log.info("收到消息确认 from user: {}, messageId: {}", 
                principal.getName(), message.getMessageId());
        // 这里可以记录消息确认状态
    }

    /**
     * 处理用户状态更新
     */
    @MessageMapping("/status")
    @SendToUser("/queue/status")
    public WebSocketMessageDto handleStatusUpdate(@Payload WebSocketMessageDto message, 
                                                 Principal principal) {
        log.info("用户状态更新 from user: {}, status: {}", 
                principal.getName(), message.getContent());
        
        return WebSocketMessageDto.builder()
                .messageId(UUID.randomUUID().toString())
                .type(WebSocketMessageType.SYSTEM_NOTIFICATION)
                .title("状态更新")
                .content("状态已更新")
                .timestamp(LocalDateTime.now())
                .priority(2)
                .requiresAck(false)
                .build();
    }

    /**
     * 处理错误消息
     */
    @MessageMapping("/error")
    @SendToUser("/queue/error")
    public WebSocketMessageDto handleError(@Payload WebSocketMessageDto message, 
                                          Principal principal) {
        log.error("收到错误消息 from user: {}, error: {}", 
                principal.getName(), message.getContent());
        
        return WebSocketMessageDto.builder()
                .messageId(UUID.randomUUID().toString())
                .type(WebSocketMessageType.ERROR)
                .title("错误处理")
                .content("错误已记录")
                .timestamp(LocalDateTime.now())
                .priority(5)
                .requiresAck(true)
                .build();
    }
}
